# pip install fastmcp

from fastmcp import FastMCP
import requests

mcp = FastMCP("MyServer")

@mcp.resource(uri="datasource://sites", description="List supported websites")
def get_sources() -> str:
    """List supported websites"""
    return f"https://www.opencsg.com"

@mcp.tool(name="top_models", description="List the most popular models")
def top_models(num: int) -> str:
    """List the most popular models"""
    resp = requests.get(f"https://hub.opencsg.com/api/v1/models?page=1&per={num}&search=&sort=most_download")
    data = resp.json()
    models = []
    for model in data['data']:
        models.append(f"The model {model['path']} has been downloaded {model['downloads']} times.")
    models_str = ','.join(models)
    return f"{models_str}"

@mcp.prompt(name="get_prompt", description="Get query prompt")
def get_prompt(num: int) -> str:
    """Get query prompt"""
    return f"Please find the top {num} models order by download count."

if __name__ == "__main__":
    # Start an HTTP server on port 8000
    # mcp.run(transport="http", host="127.0.0.1", port=8000)
    # mcp.run(transport="sse", host="0.0.0.0", port=8000)
    mcp.run(transport="streamable-http", host="0.0.0.0", port=8000)
